import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app_05/action/states/dark_states.dart';
import 'package:flutter_app_05/action/states/data_manager.dart';
import 'package:flutter_app_05/action/states/global.dart';
import 'package:flutter_app_05/data/model.dart';
import 'package:flutter_app_05/utils/MyTheme.dart';
import 'package:flutter_app_05/widget/switch.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:provider/provider.dart';
import 'package:flutter_app_05/widget/sdk_extends.dart';


class Padecalone extends StatefulWidget{
  @override
  _PadecaloneInState createState() => _PadecaloneInState();

}

class _PadecaloneInState extends State<Padecalone>{
  PageController mPageController = PageController(initialPage: 0);
  GuanJingJson guanJingJson;
  int page=0;
  TextEditingController areaController = new TextEditingController();
  TextEditingController flowController = new TextEditingController();
  TextEditingController area2Controller = new TextEditingController();
  TextEditingController intempController = new TextEditingController();
  TextEditingController dtempController = new TextEditingController();
  String html="<p/>";
  @override
  Widget build(BuildContext context) {
    return Consumer<DartState>(builder: (context, categoryState, child) {
      return columns(categoryState);
    });
  }

   Widget con01(){
    return  Container(
      // margin: EdgeInsets.all(10.0),
      // padding: EdgeInsets.all(15),
      // decoration: new BoxDecoration(
      //     color: Colors.white,
      //     borderRadius: new BorderRadius.circular((10.0))),
      alignment: Alignment.center,
      height: 100,
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Text("*",style: TextStyle(color: Colors.red),),
              Expanded(child:  Text(
                "供暖面积",
                style: TextStyle(color: MyTheme(context).getColor('colorBlack')),
              )),
              Container(
                width: 100.0,
                child: TextField(
                  style: TextStyle(color: Colors.green),
                  textAlign: TextAlign.right,
                  maxLength: 100,
                  decoration: null,
                  controller: areaController,
                  inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
                ),
              ),
              Text("m²",style: TextStyle(color: MyTheme(context).getColor('colorBlack')),),
            ],
          ),
          Container(
            padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
            child: Divider(
              height: 1.0,
              indent: 0.0,
              color: MyTheme(context).getColor("line_color"),
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Text("*",style: TextStyle(color: Colors.red),),
              Expanded(child:  Text("预供流量",style: TextStyle(color: MyTheme(context).getColor('colorBlack')),)),
              Container(
                width: 100.0,
                child: TextField(
                  textAlign: TextAlign.right,
                  style: TextStyle(color: MyTheme(context).getColor('colorBlack')),
                  maxLength: 100,
                  decoration: null,
                  controller: flowController,
                  inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
                ),
              ),
              Text("L/m²",style: TextStyle(color:MyTheme(context).getColor('colorBlack')),),
            ],
          ),
          Container(
            padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
            child: Divider(
              height: 1.0,
              indent: 0.0,
              color: MyTheme(context).getColor("line_color"),
            ),
          ),
        ],
      ),
    );
   }

   Widget con02(){
    return  Container(
      // margin: EdgeInsets.all(10.0),
      // padding: EdgeInsets.all(15),
      // decoration: new BoxDecoration(
      //     color: Colors.white,
      //     borderRadius: new BorderRadius.circular((10.0))),
      alignment: Alignment.center,
      height: 200,
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Text("*",style: TextStyle(color: Colors.red),),
              Expanded(child:  Text("供暖面积",style: TextStyle(color: MyTheme(context).getColor('colorBlack')),)),
              Container(
                width: 100.0,
                child: TextField(
                  textAlign: TextAlign.right,
                  style: TextStyle(color: Colors.green),
                  maxLength: 100,
                  decoration: null,
                  controller: area2Controller,
                  inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
                ),
              ),
              Text("m²",style: TextStyle(color: MyTheme(context).getColor('colorBlack')),),
            ],
          ),
          Container(
            padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
            child: Divider(
              height: 1.0,
              indent: 0.0,
              color: MyTheme(context).getColor("line_color"),
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Expanded(child:  Text("  热指标",style: TextStyle(color: MyTheme(context).getColor('colorBlack')),)),
              Container(
                width: 100.0,
                child: TextField(
                  textAlign: TextAlign.right,
                  style: TextStyle(color: MyTheme(context).getColor('colorBlack')),
                  maxLength: 100,
                  decoration: null,
                  controller: intempController,
                  inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
                ),
              ),
              Text("W/m²",style: TextStyle(color: MyTheme(context).getColor('colorBlack')),),
            ],
          ),
          Container(
            padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
            child: Divider(
              height: 1.0,
              indent: 0.0,
              color: MyTheme(context).getColor("line_color"),
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Expanded(child:  Text("  设计温差",style: TextStyle(color: MyTheme(context).getColor('colorBlack')),)),
              Container(
                width: 100.0,
                child: TextField(
                  textAlign: TextAlign.right,
                  style: TextStyle(color: MyTheme(context).getColor('colorBlack')),
                  maxLength: 100,
                  decoration: null,
                  controller:dtempController ,
                  inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
                ),
              ),
              Text("℃",style: TextStyle(color: MyTheme(context).getColor('colorBlack')),),
            ],
          ),
          Container(
            padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
            child: Divider(
              height: 1.0,
              indent: 0.0,
              color: MyTheme(context).getColor("line_color"),
            ),
          ),
        ],
      ),
    );
}

  Widget columns(DartState categoryState) {
    return
      Container(
        color: MyTheme(context).getColor("bg"),
          child: Column(
            children: [
              Column(
                children: [
                  Container(
                    margin: EdgeInsets.all(10.0),
                    padding: EdgeInsets.all(15),
                    decoration: new BoxDecoration(
                        color: MyTheme(context).getColor("white"),
                        borderRadius: new BorderRadius.circular((10.0))),
                    child: Column(
                      children: [
                        Container(
                          alignment: Alignment.center,
                          // width: 220,
                          height: 180,
                          child:  PageView.builder(
                            itemCount: 2,
                            controller: mPageController,
                            onPageChanged: (index) {
                              print(index);
                              guanJingJson=null;
                              setState(() {});
                            },
                            itemBuilder: (BuildContext context, int index) {
                              print(index);
                              page=index;
                              return index==0?con01():con02();
                            },
                          ),
                        ),

                        SwitchButton(
                          width: 280,
                          height: 40,
                          controller: mPageController,
                          tabs: ['流量计算', '精准计算'],
                          selectColor: Colors.green,
                          unSelectColor:Colors.white30,
                          currentIndex: mPageController.hasClients ?mPageController.page?.round(): mPageController.initialPage,
                        ),
                      ],
                    ),
                  ),
                 Center(child: Html(

                     data: html),

                 ),

                ],
              ),


              Expanded(
                  child: Stack(
                    children: [
                      Positioned(
                        child: Container(
                          height: 60.0,
                          alignment: Alignment.bottomCenter,
                          child: Row(
                            children: [
                              Expanded(
                                child: InkWell(
                                  child: Container(
                                      color: MyTheme(context).getColor("white"),
                                    child: Center(
                                      child: Text(
                                        "重置",
                                        style: new TextStyle(fontSize: 25.0,color:MyTheme(context).getColor('text_color')),
                                      ),
                                    ),
                                  ),
                                  onTap: () {
                                    setState(() {
                                      if(page==0){
                                        areaController.text="";
                                        flowController.text="";
                                      }else{
                                        area2Controller.text="";
                                        intempController.text="";
                                        dtempController.text="";
                                      }
                                    });
                                  },
                                ),
                              ),
                              Expanded(
                                child: GestureDetector(
                                  child:   Container(
                                    color: Colors.green,
                                    child: Center(
                                      child: Text(
                                        "提交",
                                        style: new TextStyle(fontSize: 25.0,color: Colors.white),
                                      ),
                                    ),
                                  ),
                                  onTap: ()async{
                                    if(page==0){
                                      await getinfo(area: areaController.text,flow: flowController.text);

                                    }else{

                                      await getinfo2(area: area2Controller.text,flow: intempController.text,dtemp: dtempController.text);
                                    }

                                    setState(() {

                                    });
                                  },
                                ),
                              )
                            ],
                          ),
                        ),
                        bottom: 0,
                        left: 0,
                        right: 0,
                      ),
                    ],
                  )),
            ],
          ),
        )
      ;
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
   // _initPersonInfo();

  }

  Future<bool> getinfo({String area,String flow }) async {
    var a=await DataManager.getGj(p:{"area":area,"size":"0","flow":flow},).then((value) {
      if(value.data==null){
        html=value.msg;
      }else{
        html=value.data;
      }

     setState(() {

     });
    });

  }

  Future<bool> getinfo2({String area,String flow,String dtemp }) async {
    var a=await DataManager.getGj(p:{"area":area,"size":"1","intemp":flow,"dtemp":dtemp},).then((value) {
      print(value.data);
      if(value.data==null){
        html=value.msg;
      }else{
        html=value.data;
      }

      guanJingJson=value;
      setState(() {

      });
    });


  }
}